package demo1;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    public boolean validPalindrome(String s) {
        char[] str = s.toCharArray();
        int i = 0, j = str.length - 1;
        while (i < j && str[i] == str[j]) {
            i++;
            j--;
        }
        return check(str, i + 1, j) || check(str, i, j - 1);
    }

    public boolean check(char[] str, int i, int j) {
        while (i < j && str[i] == str[j]) {
            i++;
            j--;
        }
        return i >= j;
    }

    public List<String> ambiguousCoordinates(String s) {
        int n=s.length();
        s=s.substring(1,n-1);
        List<String> ans=new ArrayList<>();
        for(int i=1;i<n-2;i++){
            List<String> part1=findSplit(s.substring(0,i)),part2=findSplit(s.substring(i));
            for(String a:part1){for(String b:part2){ans.add("("+a+", "+b+")");}}
        }
        return ans;
    }
    List<String> findSplit(String s){
        List<String> list=new ArrayList<>();
        if(s.length()==1||s.charAt(0)!='0'){list.add(s);}
        for(int i=1;i<s.length();i++){
            String s1=s.substring(0,i),s2=s.substring(i);
            if((s1.length()==1||s.charAt(0)!='0')&&s2.charAt(s2.length()-1)!='0'){list.add(s1+"."+s2);}
        }
        return list;
    }

    List<String> res = new ArrayList<>();
    public List<String> generateParenthesis(int n) {
        dfs(n, n, "");
        return res;
    }

    private void dfs(int left, int right, String curStr) {
        if (left == 0 && right == 0) { // 左右括号都不剩余了，递归终止
            res.add(curStr);
            return;
        }

        if (left > 0) { // 如果左括号还剩余的话，可以拼接左括号
            dfs(left - 1, right, curStr + "(");
        }
        if (right > left) { // 如果右括号剩余多于左括号剩余的话，可以拼接右括号
            dfs(left, right - 1, curStr + ")");
        }
    }
}
